

#include"Matrix3x3.h"

Matrix3x3::Matrix3x3()
{
	InitializeMatrix();
}

Matrix3x3::Matrix3x3(float m00, float m01, float m02,float m10, float m11, float m12,float m20, float m21, float m22)
{
		InitializeMatrix();
		m[0][0] = m00;    m[0][1] = m01;    m[0][2] = m02;
		m[1][0] = m10;    m[1][1] = m11;    m[1][2] = m12;
		m[2][0] = m20;    m[2][1] = m21;    m[2][2] = m22;
}


Matrix3x3::Matrix3x3(const Matrix3x3& inMatrix3x3)
{
		InitializeMatrix();
		for(int i=0;i<mRows;i++)
		{
			for(int j=0;j<mCols;j++)
			{
				m[i][j] = inMatrix3x3.m[i][j];
			}
		}
}

Matrix3x3& Matrix3x3::operator=( const Matrix3x3& inMatrix)
{
	for(int i=0;i<mRows;i++)
		for(int j=0;j<mCols;j++)
			m[i][j] = inMatrix.m[i][j];

	return *this;
}
Matrix3x3& Matrix3x3::operator*(float scalar)
{
		for(int i=0;i<mRows;i++)
			for(int j=0;j<mCols;j++)
				m[i][j] *= scalar;
		return *this;
}

Matrix3x3& Matrix3x3::operator+(const Matrix3x3& inMatrix)
{
	for(int i=0;i<mRows;i++)
		for(int j=0;j<mCols;j++)
			m[i][j]+= inMatrix.m[i][j];

	return *this;
}

Matrix3x3& Matrix3x3::operator-(const Matrix3x3& inMatrix)
{
	for(int i=0;i<mRows;i++)
		for(int j=0;j<mCols;j++)
			m[i][j]-= inMatrix.m[i][j];

	return *this;
}


void Matrix3x3::InitializeMatrix()
{
		m = (float **)new float[mRows];
		for(int i=0;i<mRows;i++)
		{
			m[i] = new float[mCols];
			for(int j=0;j<mCols;j++)
			{
				m[i][j] = 0;
			}
		}
}

void Matrix3x3::SetMatrix(const Matrix3x3& inMatrix)
{
		if(m == NULL)
			InitializeMatrix();
		for(int i=0;i<mRows;i++)
		{
			for(int j=0;j<mCols;j++)
			{
				m[i][j] = inMatrix.m[i][j];
			}
		}
}

float Matrix3x3::Determinant()
{
	float det = m[0][0]*(m[1][1]*m[2][2] - m[1][2]*m[2][1]) 
		          - m[0][1]*(m[1][0]*m[2][2] - m[1][2]*m[2][0]) 
				  + m[0][2]*(m[1][0]*m[2][1] - m[1][1]*m[2][0]);

	return det;
}

Vector3 Matrix3x3::operator*(const Vector3& inVector3)
{
	Vector3 resultVector;
	resultVector.x = m[0][0] * inVector3.x + m[0][1] * inVector3.y + m[0][2] * inVector3.z;
	resultVector.y = m[1][0] * inVector3.x + m[1][1] * inVector3.y + m[1][2] * inVector3.z;
	resultVector.x = m[2][0] * inVector3.x + m[2][1] * inVector3.y + m[2][2] * inVector3.z;
	return resultVector;
}

Matrix3x3 Matrix3x3::Identity()
{
	Matrix3x3 matrix;
	matrix.InitializeMatrix();
	for(int i=0;i<mRows;i++)
	{
		for(int j=0;j<mCols;j++)
		{
			if(i == j)  
				matrix.m[i][j] = 1;
			else 
				matrix.m[i][j] = 0;
		}
	}
	return matrix;
}

Matrix3x3::~Matrix3x3()
{
	if(m != NULL)
	{
		for(int i=0;i<mRows;i++)
			delete[] m[i];
		delete[] m;
		m = NULL;
	}
}
